<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Retencion IVA</title>
<link href="estilacho.css" rel="stylesheet" type="text/css" />
</head>

<body>
<? 
include 'conexion.php';
extract($_POST);
    extract($_GET);
if ($pro!=""){
if ($rif==""){ ?>
<script language="JavaScript" type="text/JavaScript">
alert('!ELIJE UN PROVEEDOR!')
window.self.location="retencioniva.php?<? echo 'fec='.$fec.'&nombre1='.$nombre1.'&rif1='.$rif1.'&periodo='.$periodo.'&dir='.$dir.'&nombre2='.$nombre2.'&adm='.$adm.'&jef='.$jef;?>"

</script>		
<? } else{
/*$base= "ucg - copia";
	$conexion = mysql_connect("localhost","root","");
	mysql_select_db($base,$conexion);*/
	
	$unico="SELECT max(cod_iva) from iva";
	$unicoc = mysql_query($unico, $conexion); 
	$unico= mysql_fetch_array($unicoc);
	
	$unico="SELECT max(cod_iv) from iva where cod_iva='$unico[0]'";
	$unicoc = mysql_query($unico, $conexion); 
	$unicocg= mysql_fetch_array($unicoc);
	
if($unicocg[0]==$codigo and $act!=1){ 
?>
<script language="JavaScript" type="text/JavaScript">	
	<?  //mysql_query("DELETE FROM iva WHERE cod_iv='$unicocg[0]'",$conexion); 
	$act=1;?>
alert('!DISCULPA SE HA DESACTIVADO LA FUNCION DE RECARGAR LA PAGINA POR MOTIVOS DE SEGURIDAD!')
window.self.location="retencioniva1.php?<? echo 'codigo='.$codigo.'&nombre2='.$nombre2.'&rif1='.$rif1.'&nombre1='.$nombre1.'&periodo='.$periodo.'&dir='.$dir.'&rif='.$rif.'&adm='.$adm.'&jef='.$jef.'&fec='.$fec.'&act='.$act.'&pro='.$pro; ?>"

</script>
	
<?	} else {


$sqlrif="Select * from proveedor where rif='$rif'";
	     $resultadorif = mysql_query($sqlrif, $conexion); 
	     $busquedarif = mysql_fetch_row($resultadorif);
		 $nombre2=$busquedarif[1];	
		
	mysql_query("INSERT iva VALUES($cod,$codigo,'$fec','$nombre1','$rif1',$periodo,'$dir','$nombre2','$rif','$adm','$jef','','','$elab')",$conexion);
	
	$com="SELECT max(cod_iva) from iva";
	$compr = mysql_query($com, $conexion); 
	$compra = mysql_fetch_array($compr);
	$orden1=$compra[0];
	
	$com2="SELECT max(cod_iv) from iva where cod_iva='$orden1' ";
	$compr2 = mysql_query($com2, $conexion); 
	$compra2 = mysql_fetch_array($compr2);
	$orden2=$compra2[0]; 
	
	?>

<form action="retencioniva1.php" method="post">  
<table align="center" class="tablainstitucion">
<th colspan="6" bordercolor="#FFFFFF">RETENCI0N DEL IMPUESTO AL VALOR AGREGADO
<input name="orden1" type="hidden" class="boton" id="orden1" value="<? echo $orden1?>" />
   <input name="rif1" type="hidden" class="boton" id="rif1" value="<? echo $rif1?>" />
  <input name="nombre1" type="hidden" class="boton" id="nombre1" value="<? echo $nombre1?>" />
  <input name="dir" type="hidden" id="dir" value="<? echo $dir?>" />
  <input name="nombre2" type="hidden" class="boton" id="nombre2" value="<? echo $nombre2?>" />
  <input name="rif" type="hidden" class="boton" id="rif" value="<? echo $rif?>" />
  <input name="adm" type="hidden"  id="adm" value="<? echo $adm?>" />
  <input name="act" type="hidden"  id="act" value="1" />
  <input name="jef" type="hidden" class="boton" id="jef" value="<? echo $jef?>" />
  <input name="periodo" type="hidden" class="boton" id="periodo" value="<? echo $periodo?>" />
  <input name="fec" type="hidden" class="boton" id="fec" value="<? echo $fec?>" />
   <input name="pro" type="hidden" class="boton" id="pro" value="<? echo $pro?>" />
  </th>
  <tr>
  
    <td width="40%" height="17" class="subtitulo">NRO. COMPROBANTE IVA:</td>
    <td width="30%"><? echo $orden2; ?></td>
    <td width="14%" class="subtitulo"> FECHA:</td>
    <td width="16%"><? echo $fec ?></td>
  </tr>
  <tr>
    <td class="subtitulo">  REGISTRO DE INFORMACI&Oacute;N DEL AGENTE DE RETENCION</td>
    <td><? echo $rif1; ?></td>
    <td class="subtitulo">  PERIODO FISCAL</td>
    <td><? echo $periodo; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">NOMRE O RAZON SOCIAL DE AGENTE DE RETENCION</td>
    <td colspan="3"><? echo $nombre1; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">DIRECCI0N FISCAL DEL AGENTE DE RETENCION </td>
    <td colspan="3">
      <? echo $dir; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">NOMBRE O RAZON SOCIAL DEL SUJETO RETENIDO</td>
    <td colspan="3"><? echo $nombre2; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">REGISTRO DE INFORMACI&Oacute;N FISCAL DEL SUJETO RETENIDO (RIF)</td>
    <td colspan="3"><? echo $rif; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">ELABORADO:</td>
    <td colspan="3"><? echo $adm; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">APROBADO:</td>
    <td colspan="3"><? echo $jef; ?></td>
  </tr>
  <tr>
    <td class="subtitulo">FACTURA:</td>
    <td colspan="3">
    <select name="fac" class="combobox" id="fac" onchange="submit()">
    <option value="">Factura No</option>
	<?   $sql="Select cod_p from pago where rif='$rif' and anulado!='1'";
	     $resultado = mysql_query($sql, $conexion); 
		 while ($cd= mysql_fetch_array($resultado)){		  
		  	  
		  $sql3="Select factura from id_pago where cod_p='$cd[0]' group by factura";
	      $resul3 = mysql_query($sql3, $conexion);  
		  
		  while ($busqueda = mysql_fetch_array($resul3)) { ?>
        <option value="<? echo $busqueda[0];  ?>"><? echo $busqueda[0];?></option>       
        <? } }?>
      </select>

Retencion del IVA
<input name="ret" type="text" class="entrada" id="ret" value="75" size="3" maxlength="3" />   </td>
  </tr>
</table>
</form>
<p>&nbsp;</p>
<?  $sql2="Select fecha from causado where factura='$fac5'";
	      $resul2 = mysql_query($sql2, $conexion); 
		  $fec2= mysql_fetch_array($resul2);
		  
    if($fac<>""){ 
	//$fact=$fac;
	echo "Elije un IVA para la Retencion"; echo"<br>";
	$facl="SELECT cod_c from id_pago where factura='$fac' group by cod_c";
	$fac2 = mysql_query($facl, $conexion); 
	while ($fac9 = mysql_fetch_array($fac2)){
	$fac3=$fac9[0];
	
	
	$iva3="SELECT iva from id_compra where cod_c='$fac3' and iva<>'0' and iva<>'$ivita' group by iva";
	$iva4 = mysql_query($iva3, $conexion); 
	while ($iva5 = mysql_fetch_array($iva4)){
	
	$ivita=$iva5[0];   }
	 
	echo "<a href='retencioniva1.php?iva5=".$ivita."&ret=".$ret."&fac5=".$fac."&fec=".$fec."&nombre1=".$nombre1."&rif1=".$rif1."&rif=".$rif."&periodo=".$periodo."&dir=".$dir."&nombre2=".$nombre2."&adm=".$adm."&jef=".$jef."&orden1=".$orden1."&pro=".$pro."'>".$ivita."</a><br>";
	
	}}//}}
	
	
	//$fac3=$fac3;
	if($fac5){
		extract($_GET);
		
	$fac6="SELECT cod_c from id_pago where factura='$fac5' group by cod_c";
	$fac8 = mysql_query($fac6, $conexion); 
	while ($fac7 = mysql_fetch_array($fac8)){
	
	$fac4=$fac7[0];		
	$iva5=round($iva5,2);	
	
	$exc="SELECT sum(pre_unit*cantidad) from id_compra where cod_c='$fac4' and iva='0'";
	$exc1 = mysql_query($exc, $conexion); 
	$exc2 = mysql_fetch_array($exc1);
	$exc3=$exc3+$exc2[0];
	$exc3=round($exc3,2);
	//$exc4=$exc4+$exc3;
	
	$noexc="SELECT sum(pre_unit*cantidad) from id_compra where cod_c='$fac4' and iva='$iva5'";
	$noexc1 = mysql_query($noexc, $conexion); 
	$noexc2 = mysql_fetch_array($noexc1);
	$noexc3=$noexc3+$noexc2[0];
	$noexc3=round($noexc3,2);
	//$noexc4=$noexc4+$noexc3;
	
	/*$iva7="SELECT iva from id_compra where cod_c='$fac3' and iva='$iva5' group by iva";
	$iva8 = mysql_query($iva7, $conexion); 
	$iva9 = mysql_fetch_array($iva8);*/
	$iva10=$iva5;
	
	$imp=$noexc3*($iva10/100);
	$imp=round($imp,2);
	$imp2=$imp2+$imp;
	$retencion=$imp*($ret/100);
	$retencion=round($retencion,2);	
	
	$debitoiva=$imp+$noexc3+$exc3;
	} }
	/*else{
	
	extract($_GET);
	extract($_POST);
	$fac3=$fac3;
	$iva5=round($iva5,2); 
	
	$exc="SELECT sum(pre_unit*cantidad) from id_compra where cod_c='$fac3' and iva='0'";
	$exc1 = mysql_query($exc, $conexion); 
	$exc2 = mysql_fetch_array($exc1);
	$exc3=$exc2[0];
	$exc3=round($exc3,2);
	
	$noexc="SELECT sum(pre_unit*cantidad) from id_compra where cod_c='$fac3' and iva<>'0'";
	$noexc1 = mysql_query($noexc, $conexion); 
	$noexc2 = mysql_fetch_array($noexc1);
	$noexc3=$noexc2[0];
	$noexc3=round($noexc3,2);
	
	$iva7="SELECT iva from id_compra where cod_c='$fac3' and iva<>'0' group by iva";
	$iva8 = mysql_query($iva7, $conexion); 
	$iva9 = mysql_fetch_array($iva8);
	 $iva10=$iva9[0];
	
	$imp=$noexc3*($iva10/100);
	$imp=round($imp,2);
	$retencion=$imp*($ret/100);
	$retencion=round($retencion,2);	
	
	$debitoiva=$imp+$noexc3+$exc3;*/
	
	 ?>
<form action="insertar_detalle_iva.php" method="post" name="form2">
<table width="95%"  border="0" align="center" class="tablainstitucion">
  <tr>
    <td colspan="9" bordercolor="#FFFFFF">
      <input name="orden1" type="hidden" class="boton" id="orden1" value="<? echo $orden1?>" />
      <input name="rif1" type="hidden" class="boton" id="rif1" value="<? echo $rif1?>" />
      <input name="nombre1" type="hidden" class="boton" id="nombre1" value="<? echo $nombre1?>" />
      <input name="dir" type="hidden" id="dir" value="<? echo $dir?>" />
      <input name="nombre2" type="hidden" class="boton" id="nombre2" value="<? echo $nombre2?>" />
      <input name="rif" type="hidden" class="boton" id="rif" value="<? echo $rif  ?>" />
      <input name="adm" type="hidden"  id="adm" value="<? echo $adm?>" />
      <input name="act" type="hidden"  id="act" value="1" />
      <input name="jef" type="hidden" class="boton" id="jef" value="<? echo $jef?>" />
      <input name="periodo" type="hidden" class="boton" id="periodo" value="<? echo $periodo?>" />
      <input name="fec" type="hidden" class="boton" id="fec" value="<? echo $fec?>" />
      <input name="pro" type="hidden" class="boton" id="pro" value="<? echo $pro?>" />
      <input name="ret" type="hidden" class="boton" id="ret" value="<? echo $ret?>" /></td>
    <td colspan="3" bordercolor="#000000"><div align="center"><span class="style6 style10">COMPRAS INTERNAS O IMPORTACIONES </span></div></td>
    <td width="6%" bordercolor="#FFFFFF">&nbsp;</td>
    <td width="9%" bordercolor="#FFFFFF"><span class="style6"></span></td>
  </tr>
  <tr bordercolor="#000000">
    <td width="4%"><div align="center"><span class="style6">Oper N&ordm; </span></div></td>
    <td width="6%"><div align="center"><span class="style6">Fecha de la Factura </span></div></td>
    <td width="14%"><div align="center"><span class="style6">N&ordm; Factura </span></div></td>
    <td width="4%"><div align="center"><span class="style6">N&ordm; N.D </span></div></td>
    <td width="4%"><div align="center"><span class="style6">N&ordm; N.C </span></div></td>
    <td width="8%"><div align="center"><span class="style6">Tipo de Trans </span></div></td>
    <td width="9%"><div align="center"><span class="style6">N&ordm; de Factura Afectada </span></div></td>
    <td width="6%"><div align="center"><span class="style6">Total Compras incluendo IVA </span></div></td>
    <td width="8%"><div align="center"><span class="style6">Compras sin derecho a cr&eacute;dito IVA </span></div></td>
    <td width="6%"><div align="center"><span class="style6">Base Imponible </span></div></td>
    <td width="8%"><div align="center"><span class="style6">% &nbsp;Alicuota</span></div></td>
    <td width="8%"><div align="center"><span class="style6">Impuesto IVA </span></div></td>
    <td><div align="center"><span class="style6">IVA Retenido </span></div></td>
    <td>&nbsp;</td>
  </tr>
   
  <tr bordercolor="#000000">
    <td><div align="right">
          <input name="n_ret" type="text" class="entrada" id="n_ret" size="2" maxlength="2"/>
    </div></td>
    <td>
      <input name="fecha" type="text" class="entrada" id="fecha" value="<? echo $fec2[0]; ?>" size="12" maxlength="20" />   </td>
    <td><div align="center"><? echo $fac5; ?></div></td>
    <td><div align="center">
      <input name="nd" type="text" class="entrada" id="nd" size="4" maxlength="20" />
    </div></td>
    <td><div align="center">
      <input name="nc" type="text" class="entrada" id="nc" size="4" maxlength="20" />
    </div></td>
    <td><div align="center">
      <input name="tipo_trans" type="text" class="entrada" id="tipo_trans" size="10" maxlength="100" />
    </div></td>
    <td><div align="center">
      <input name="fac_afec" type="hidden" class="boton" id="fac_afec" value="<? echo $fac5 ?>" />
      <? echo $fac5; ?></div></td>
     <p></p>

    <td><div align="center">
      <input name="debitoiva" type="hidden" class="boton" id="debitoiva" value="<? echo $debitoiva?>" />
      <? echo $debitoiva; ?></div></td>
    <td><div align="center">
      <input name="exc3" type="hidden" class="boton" id="exc3" value="<? echo $exc3?>" />
      <? echo $exc3; ?></div></td>
    <td><div align="center">
      <input name="noexc3" type="hidden" class="boton" id="noexc3" value="<? echo $noexc3?>" />
      <? echo $noexc3; ?></div></td>
    <td><div align="center">
      <input name="iva10" type="hidden" class="boton" id="iva10" value="<? echo $iva10?>" />
      <? echo $iva10; ?></div></td>
    <td><div align="center">
      <input name="imp" type="hidden" class="boton" id="imp" value="<? echo $imp?>" />
      <? echo $imp; ?></div></td>
    <td><div align="center">
      <input name="retencion" type="hidden" class="boton" id="retencion" value="<? echo $retencion?>" />
      <? echo $retencion; ?></div></td>
    <td>
     <input name="agre" type="submit" class="boton" id="agre" value="Agregar"/>   </td>
  </tr>
 <input name="pro" type="hidden" value="<? echo $pro; ?>">
</table>
</form>
<p>&nbsp;</p>
<?


$id_iva="SELECT * from id_iva where cod_iva='$orden1'";
	$id_iva1 = mysql_query($id_iva, $conexion); 	
	?>
<table width="95%"  border="1" align="center" class="tablainstitucion">
  <tr>
    <td colspan="9" bordercolor="#FFFFFF">&nbsp;</td>
    <td colspan="3" bordercolor="#000000"><div align="center"><span class="style6 style10">COMPRAS INTERNAS O IMPORTACIONES </span></div></td>
    <td width="6%" bordercolor="#FFFFFF">&nbsp;</td>
    <td width="9%" bordercolor="#FFFFFF"><span class="style6"></span></td>
  </tr>
  <tr bordercolor="#000000">
    <td width="4%"><div align="center"><span class="style6">Oper N&ordm; </span></div></td>
    <td width="6%"><div align="center"><span class="style6">Fecha de la Factura </span></div></td>
    <td width="14%"><div align="center"><span class="style6">N&ordm; Factura </span></div></td>
    <td width="4%"><div align="center"><span class="style6">N&ordm; N.D </span></div></td>
    <td width="4%"><div align="center"><span class="style6">N&ordm; N.C </span></div></td>
    <td width="8%"><div align="center"><span class="style6">Tipo de Trans </span></div></td>
    <td width="9%"><div align="center"><span class="style6">N&ordm; de Factura Afectada </span></div></td>
    <td width="6%"><div align="center"><span class="style6">Total Compras incluendo IVA </span></div></td>
    <td width="8%"><div align="center"><span class="style6">Compras sin derecho a cr&eacute;dito IVA </span></div></td>
    <td width="6%"><div align="center"><span class="style6">Base Imponible </span></div></td>
    <td width="8%"><div align="center"><span class="style6">% &nbsp;Alicuota</span></div></td>
    <td width="8%"><div align="center"><span class="style6">Impuesto IVA </span></div></td>
    <td><div align="center"><span class="style6">IVA Retenido </span></div></td>
    <td>ELIMINAR</td>
  </tr>
  <? while ($id_iva2 = mysql_fetch_array($id_iva1)){ ?>
  <tr bordercolor="#000000">
  
    <td><div align="center"><? echo $id_iva2[2]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[3]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[4]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[5]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[6]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[7]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[8]; ?></div></td>    
    <td><div align="center"><? echo $id_iva2[9]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[10]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[11]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[12]; ?>%</div></td>
    <td><div align="center"><? echo $id_iva2[13]; ?></div></td>
    <td><div align="center"><? echo $id_iva2[14]; ?></div></td>
    <td><label>
      <div align="center"><img src="x2.JPG" alt=" Eliminar" width="20" height="18" align="absmiddle" onclick="window.location='retencioniva1.php?<? echo 'orden1='.$orden1.'&iva5='.$ivita.'&fac='.$fac.'&fac5='.$fac.'&ret='.$ret.'&fec='.$fec.'&nombre1='.$nombre1.'&rif1='.$rif1.'&rif='.$rif.'&periodo='.$periodo.'&dir='.$dir.'&nombre2='.$nombre2.'&adm='.$adm.'&jef='.$jef.'&ord='.$id_iva2[0].'&pro='.$pro; ?>'"/>
          </label>
      </div></td>
  </tr>
  <? } ?>
  </table>
  <? if ($ord){
    extract($_POST);
    extract($_GET);
    $act=1;
	/*$base= "ucg - copia";
	$conexion=mysql_connect("localhost","root","");
	mysql_select_db($base,$conexion);*/	
	mysql_query("DELETE FROM id_iva WHERE id_iva='$ord' and cod_iva='$orden1'",$conexion); 	
	mysql_close($conexion);
?>
<script language='JavaScript'>
	window.self.location="retencioniva1.php?<? echo 'pro='.$pro.'&orden1='.$orden1.'&iva5='.$ivita.'&ret='.$ret.'&fec='.$fec.'&nombre1='.$nombre1.'&rif1='.$rif1.'&rif='.$rif.'&periodo='.$periodo.'&dir='.$dir.'&nombre2='.$nombre2.'&adm='.$adm.'&jef='.$jef.'&act=1';?>"
  </script>
 <? } ?>


<div align="center">
  <input name="menu" type="button" class="boton" value="Finalizar Retencion" onclick="window.location='index1.php'" />
  <input name="menu2" type="submit" class="boton" value="Cancelar Orden" onclick="window.location='Borrar_orden_iva.php?<? echo 'orden1='.$orden1;  ?>'" />
</div>

<? }}}?>
</body>
</html>
